home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / LockEmUp / LockemUp.jar / LockemUp / LevelScreen.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-03-05  |  8.3 KB  |  460 lines

  1. package LockemUp;
  2.  
  3. import com.siemens.mp.game.ExtendedImage;
  4. import com.siemens.mp.game.GraphicObjectManager;
  5. import com.siemens.mp.game.Sprite;
  6. import javax.microedition.lcdui.Canvas;
  7. import javax.microedition.lcdui.CommandListener;
  8. import javax.microedition.lcdui.Font;
  9. import javax.microedition.lcdui.Graphics;
  10. import javax.microedition.lcdui.Image;
  11. import javax.microedition.lcdui.List;
  12. import javax.microedition.rms.RecordStore;
  13.  
  14. public class LevelScreen extends BaseScreen {
  15.    private static final int GAME_SCREEN_WIDTH = 96;
  16.    private static final int GAME_SCREEN_HEIGHT = 80;
  17.    private static final int BLINK_TIMER = 4;
  18.    private static final int[] LEVELMAPCOUNT = new int[]{5, 10, 5, 5, 5, 10};
  19.    private static final String MENU_DATA = "res\\menu.bin";
  20.    private static final String NUM_TILES = "res\\num.bin";
  21.    private static final String[] LEVELNAME = new String[]{"Learning", "Beginning", "Switch", "Death", "Jumper", "Master"};
  22.    private static final String GAMERECORD = "REC";
  23.    private byte[] records;
  24.    private boolean modified;
  25.    private int maxLevel;
  26.    private int maxMap;
  27.    private int level = 0;
  28.    private int map = 0;
  29.    private long timer;
  30.    private CommandListener listener;
  31.    private int state;
  32.    private boolean running;
  33.    private boolean readyToPaint;
  34.    private Image gameScreenImage;
  35.    private ExtendedImage gameScreen;
  36.    private GraphicObjectManager gfxManager;
  37.    private Sprite levelSpr;
  38.    private Sprite mapSpr;
  39.    private boolean blink;
  40.    private boolean visible;
  41.    private int blinkTimer;
  42.    private boolean repaintFlag;
  43.    private byte[] pixels;
  44.    private int menuShow;
  45.    private Image menu;
  46.    private ExtendedImage menuEx;
  47.  
  48.    public LevelScreen() {
  49.       try {
  50.          this.gameScreenImage = Image.createImage(96, 80);
  51.          this.gameScreen = new ExtendedImage(this.gameScreenImage);
  52.          this.gfxManager = new GraphicObjectManager();
  53.          this.pixels = ((BaseScreen)this).ReadByteArray("res\\num.bin", 42, 0);
  54.          byte[] var2 = new byte[42];
  55.  
  56.          for(int var1 = 0; var1 < 42; ++var1) {
  57.             var2[var1] = -2;
  58.          }
  59.  
  60.          this.levelSpr = new Sprite(this.pixels, 0, 8, 7, var2, 0, 6);
  61.          Object var6 = null;
  62.          this.pixels = ((BaseScreen)this).ReadByteArray("res\\num.bin", 77, 0);
  63.          byte[] var7 = new byte[77];
  64.  
  65.          for(int var5 = 0; var5 < 77; ++var5) {
  66.             var7[var5] = -2;
  67.          }
  68.  
  69.          this.mapSpr = new Sprite(this.pixels, 0, 8, 7, var7, 0, 11);
  70.          Object var8 = null;
  71.          this.gfxManager.addObject(this.levelSpr);
  72.          this.gfxManager.addObject(this.mapSpr);
  73.          this.pixels = ((BaseScreen)this).ReadByteArray("res\\menu.bin", 960, 0);
  74.          this.menu = Image.createImage(24, 8);
  75.          this.menuEx = new ExtendedImage(this.menu);
  76.          this.menuEx.setPixels(((BaseScreen)this).ReadByteArray("res\\menu_sprite.bin", 24, 0), 0, 0, 24, 8);
  77.       } catch (Exception var4) {
  78.          System.out.println("Exception: " + var4);
  79.       }
  80.  
  81.       this.modified = false;
  82.       this.records = new byte[80];
  83.       this.timer = 0L;
  84.       this.blink = false;
  85.       this.visible = true;
  86.       this.state = 0;
  87.       this.readyToPaint = true;
  88.       this.repaintFlag = true;
  89.    }
  90.  
  91.    public void Dispose() {
  92.       this.menuEx = null;
  93.       this.gameScreen = null;
  94.       this.gfxManager = null;
  95.    }
  96.  
  97.    public void loadRecords() {
  98.       for(int var1 = 0; var1 < 80; ++var1) {
  99.          this.records[var1] = 0;
  100.       }
  101.  
  102.       try {
  103.          RecordStore var3 = RecordStore.openRecordStore("REC", true);
  104.          byte[] var4 = new byte[2];
  105.          if (var3.getNumRecords() > 0) {
  106.             var3.getRecord(1, this.records, 0);
  107.             var3.getRecord(2, var4, 0);
  108.             this.maxLevel = var4[0];
  109.             this.level = this.maxLevel - 1;
  110.             this.maxMap = var4[1];
  111.             this.map = this.maxMap - 1;
  112.          } else {
  113.             var4[0] = 1;
  114.             var4[1] = 1;
  115.             var3.addRecord(this.records, 0, 80);
  116.             var3.addRecord(var4, 0, 2);
  117.             this.maxLevel = 1;
  118.             this.level = 0;
  119.             this.maxMap = 1;
  120.             this.map = 0;
  121.          }
  122.  
  123.          var3.closeRecordStore();
  124.       } catch (Exception var5) {
  125.          System.out.println("Exception: " + var5);
  126.       }
  127.  
  128.    }
  129.  
  130.    public void saveRecords() {
  131.       try {
  132.          if (this.modified) {
  133.             RecordStore var2 = RecordStore.openRecordStore("REC", true);
  134.             byte[] var3 = new byte[]{(byte)this.maxLevel, (byte)this.maxMap};
  135.             if (var2.getNumRecords() > 0) {
  136.                var2.setRecord(1, this.records, 0, 80);
  137.                var2.setRecord(2, var3, 0, 2);
  138.             } else {
  139.                var2.addRecord(this.records, 0, 80);
  140.                var2.addRecord(var3, 0, 2);
  141.             }
  142.  
  143.             var2.closeRecordStore();
  144.             this.modified = false;
  145.          }
  146.       } catch (Exception var4) {
  147.          System.out.println("Exception: " + var4);
  148.       }
  149.  
  150.    }
  151.  
  152.    public void resetStateValue() {
  153.       this.state = 0;
  154.       this.repaintFlag = true;
  155.       this.gameScreen.clear((byte)0);
  156.    }
  157.  
  158.    public void setStateValue(int var1) {
  159.       this.state = var1;
  160.       this.repaintFlag = false;
  161.       this.gameScreen.clear((byte)0);
  162.       Font var2 = Font.getFont(0, 1, 8);
  163.       this.gameScreenImage.getGraphics().setFont(var2);
  164.       int var3 = var2.stringWidth("Please wait...");
  165.       int var4 = var2.getHeight();
  166.       int var5 = (96 - var3) / 2;
  167.       int var6 = (80 - var4) / 2;
  168.       this.gameScreenImage.getGraphics().drawString("Please wait...", var5, var6, 20);
  169.       var5 -= 2;
  170.       var6 -= 2;
  171.       this.gameScreenImage.getGraphics().drawRect(var5, var6, var3 + 3, var4 + 3);
  172.       var5 -= 2;
  173.       var6 -= 2;
  174.       this.gameScreenImage.getGraphics().drawRect(var5, var6, var3 + 7, var4 + 7);
  175.       this.gameScreen.blitToScreen(0, 0);
  176.    }
  177.  
  178.    public void startTimer() {
  179.       this.timer = System.currentTimeMillis();
  180.    }
  181.  
  182.    public void stopTimer() {
  183.       this.timer = (System.currentTimeMillis() - this.timer) / 1000L;
  184.       if (this.timer > 5999L) {
  185.          this.timer = 5999L;
  186.       }
  187.  
  188.       if ((long)this.getBestTimeOfCurrentLevel() > this.timer) {
  189.          this.records[2 * this.getMapNum()] = (byte)((int)(this.timer / 60L));
  190.          this.records[2 * this.getMapNum() + 1] = (byte)((int)(this.timer % 60L));
  191.          this.modified = true;
  192.          if (this.level == this.maxLevel - 1 && this.map == this.maxMap - 1) {
  193.             if (this.maxMap < LEVELMAPCOUNT[this.level]) {
  194.                ++this.maxMap;
  195.             } else if (this.maxLevel < 6) {
  196.                ++this.maxLevel;
  197.                this.maxMap = 1;
  198.             }
  199.          }
  200.       }
  201.  
  202.    }
  203.  
  204.    public void startBlink() {
  205.       this.blink = true;
  206.       this.blinkTimer = 4;
  207.       this.visible = true;
  208.    }
  209.  
  210.    public void stopBlink() {
  211.       this.blink = false;
  212.       this.visible = true;
  213.    }
  214.  
  215.    public String getTimeOfThisTurn() {
  216.       int var2 = (int)(this.timer / 60L);
  217.       String var1;
  218.       if (var2 < 10) {
  219.          var1 = "0" + String.valueOf(var2);
  220.       } else {
  221.          var1 = String.valueOf(var2);
  222.       }
  223.  
  224.       var2 = (int)(this.timer % 60L);
  225.       if (var2 < 10) {
  226.          var1 = var1 + ":0" + var2;
  227.       } else {
  228.          var1 = var1 + ":" + var2;
  229.       }
  230.  
  231.       return var1;
  232.    }
  233.  
  234.    public int getReturnValue() {
  235.       return this.state < 40 ? this.getMapNum() : this.state;
  236.    }
  237.  
  238.    public int nextMap() {
  239.       if (this.map < LEVELMAPCOUNT[this.level] - 1) {
  240.          ++this.map;
  241.       } else {
  242.          this.map = 0;
  243.          this.level = (this.level + 1) % 6;
  244.       }
  245.  
  246.       return this.getMapNum();
  247.    }
  248.  
  249.    public int getMapNum() {
  250.       return this.computeMapNum(this.level, this.map);
  251.    }
  252.  
  253.    public String getStringOfBestTime() {
  254.       if (this.level < this.maxLevel && (this.level != this.maxLevel - 1 || this.map < this.maxMap)) {
  255.          if (this.level != this.maxLevel - 1 || this.map != this.maxMap - 1 || this.maxLevel == 6 && this.maxMap == 10) {
  256.             if (this.level == 5 && this.map == 9 && this.records[78] == 0 && this.records[79] == 0) {
  257.                return "NO REC";
  258.             } else {
  259.                int var3 = this.getMapNum();
  260.                byte var2 = this.records[2 * var3];
  261.                String var1;
  262.                if (var2 < 10) {
  263.                   var1 = "0" + String.valueOf(var2);
  264.                } else {
  265.                   var1 = String.valueOf(var2);
  266.                }
  267.  
  268.                var2 = this.records[2 * var3 + 1];
  269.                if (var2 < 10) {
  270.                   var1 = var1 + ":0" + var2;
  271.                } else {
  272.                   var1 = var1 + ":" + var2;
  273.                }
  274.  
  275.                return var1;
  276.             }
  277.          } else {
  278.             return "NO REC";
  279.          }
  280.       } else {
  281.          return "LOCKED";
  282.       }
  283.    }
  284.  
  285.    public void run() {
  286.       int var1 = 11;
  287.       this.running = true;
  288.  
  289.       while(this.running) {
  290.          try {
  291.             Thread.sleep(20L);
  292.             if (this.blink) {
  293.                --this.blinkTimer;
  294.                if (this.blinkTimer <= 0) {
  295.                   this.visible = !this.visible;
  296.                   this.blinkTimer = 4;
  297.                   this.repaintFlag = true;
  298.                }
  299.             }
  300.  
  301.             if (this.repaintFlag) {
  302.                Font var2 = Font.getFont(0, 0, 8);
  303.                this.gameScreen.setPixels(this.pixels, 0, 0, 96, 80);
  304.                this.gameScreenImage.getGraphics().setColor(16777215);
  305.                if (LEVELMAPCOUNT[this.level] == 5) {
  306.                   this.gameScreenImage.getGraphics().fillRect(35, 49, 50, 9);
  307.                }
  308.  
  309.                this.gameScreenImage.getGraphics().setFont(var2);
  310.                this.gameScreenImage.getGraphics().drawString(LEVELNAME[this.level], 36, 20, 20);
  311.                if (this.visible) {
  312.                   this.gameScreenImage.getGraphics().drawString(this.getStringOfBestTime(), 48, 66, 20);
  313.                }
  314.  
  315.                this.gameScreenImage.getGraphics().setColor(0);
  316.                if (this.level >= this.maxLevel) {
  317.                   var1 = 0;
  318.                } else if (this.level == this.maxLevel - 1) {
  319.                   var1 = this.maxMap;
  320.                } else {
  321.                   var1 = 11;
  322.                }
  323.  
  324.                while(var1 < LEVELMAPCOUNT[this.level]) {
  325.                   this.gameScreenImage.getGraphics().fillRect(38 + var1 % 5 * 10, 39 + var1 / 5 * 12, 5, 5);
  326.                   ++var1;
  327.                }
  328.  
  329.                this.drawMapNum();
  330.                this.drawLevelNum();
  331.                this.gfxManager.paint(this.gameScreen, 0, 0);
  332.                this.repaintFlag = false;
  333.             }
  334.  
  335.             ((Canvas)this).repaint();
  336.          } catch (Exception var3) {
  337.          }
  338.  
  339.          if (this.state != 0) {
  340.             this.running = false;
  341.          }
  342.       }
  343.  
  344.    }
  345.  
  346.    public void paint(Graphics var1) {
  347.       boolean var2 = true;
  348.       if (this.gameScreen != null && this.readyToPaint) {
  349.          try {
  350.             this.gameScreen.blitToScreen(0, 0);
  351.          } catch (Exception var4) {
  352.             System.out.println("Exception: " + var4);
  353.          }
  354.       }
  355.  
  356.       if (this.state == 0) {
  357.          if ((this.menuShow++ & 8) != 0) {
  358.             this.menuEx.blitToScreen(80, 67);
  359.          } else {
  360.             var1.setColor(255, 255, 255);
  361.             var1.fillRect(96, 67, 5, 8);
  362.          }
  363.       }
  364.  
  365.    }
  366.  
  367.    public void setCommandListener(CommandListener var1) {
  368.       super.setCommandListener(var1);
  369.       this.listener = var1;
  370.    }
  371.  
  372.    protected void keyPressed(int var1) {
  373.       if (var1 == -12) {
  374.          this.listener.commandAction(List.SELECT_COMMAND, this);
  375.       } else if (!this.repaintFlag) {
  376.          this.repaintFlag = true;
  377.          int var2 = ((Canvas)this).getGameAction(var1);
  378.          switch (var2) {
  379.             case 1:
  380.                if (this.level > 0) {
  381.                   --this.level;
  382.                }
  383.  
  384.                if (this.map >= LEVELMAPCOUNT[this.level]) {
  385.                   this.map = LEVELMAPCOUNT[this.level] - 1;
  386.                }
  387.  
  388.                this.stopBlink();
  389.                break;
  390.             case 2:
  391.                if (this.map > 0) {
  392.                   --this.map;
  393.                }
  394.  
  395.                this.stopBlink();
  396.             case 3:
  397.             case 4:
  398.             default:
  399.                break;
  400.             case 5:
  401.                if (this.map < LEVELMAPCOUNT[this.level] - 1) {
  402.                   ++this.map;
  403.                }
  404.  
  405.                this.stopBlink();
  406.                break;
  407.             case 6:
  408.                if (this.level < 5) {
  409.                   ++this.level;
  410.                }
  411.  
  412.                if (this.map >= LEVELMAPCOUNT[this.level]) {
  413.                   this.map = LEVELMAPCOUNT[this.level] - 1;
  414.                }
  415.  
  416.                this.stopBlink();
  417.          }
  418.  
  419.       }
  420.    }
  421.  
  422.    private void drawLevelNum() {
  423.       this.levelSpr.setPosition(9, 19 + this.level * 10);
  424.       this.levelSpr.setFrame(this.level);
  425.    }
  426.  
  427.    private void drawMapNum() {
  428.       this.mapSpr.setPosition(37 + this.map % 5 * 10, 38 + this.map / 5 * 12);
  429.       if (this.level >= this.maxLevel) {
  430.          this.mapSpr.setFrame(10);
  431.       } else if (this.level == this.maxLevel - 1 && this.map >= this.maxMap) {
  432.          this.mapSpr.setFrame(10);
  433.       } else {
  434.          this.mapSpr.setFrame(this.map);
  435.       }
  436.  
  437.    }
  438.  
  439.    private int getBestTimeOfCurrentLevel() {
  440.       int var1 = this.getMapNum();
  441.       var1 = this.records[2 * var1] * 60 + this.records[2 * var1 + 1];
  442.       if (var1 == 0) {
  443.          var1 = 6000;
  444.       }
  445.  
  446.       return var1;
  447.    }
  448.  
  449.    private int computeMapNum(int var1, int var2) {
  450.       int var4 = 0;
  451.  
  452.       for(int var3 = 0; var3 < var1; ++var3) {
  453.          var4 += LEVELMAPCOUNT[var3];
  454.       }
  455.  
  456.       var4 += var2;
  457.       return var4;
  458.    }
  459. }
  460.